/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 * */

// AmzServer - Tototoy's server model.
// Author: tonyjobmails@gmail.com (tonyhack).
//
// Define class TCPBufferSocketCommunicator.
//
// TCPBufferSocketCommunicator uses to send/receive TCP stream data with
// 2 buffers.
//

#include "tcp_buffer_socket_communicator.h"
#include "logging.h"

namespace amz {

TCPBufferSocketCommunicator::TCPBufferSocketCommunicator(int socket,
       CompressType compress, EncryptType encrypt)
       : TCPSocketCommunicator(socket, compress, encrypt) {}

TCPBufferSocketCommunicator::~TCPBufferSocketCommunicator() {}

int TCPBufferSocketCommunicator::Send(const char *message, int size) {
  // Calculate packet size, allocate, packet.
  int need_size = this->packet_.CalculatePacketSize(size);
  if(this->send_buffer_.PreWrite(need_size) == false) {
    LOG(ERROR) << "Failed to Send, send_buffer_ size is not enough!";
    return -1;
  }
  int packet_size = this->packet_.Packet(message, size, this->send_buffer_.WritePointer(),
         this->send_buffer_.WriteSize());
  if(packet_size <= 0) {
    return packet_size;
  }
  this->send_buffer_.WriteShift(packet_size);
  return size;
}

int TCPBufferSocketCommunicator::Receive(char *message, int max_size) {
  // Unpacket a message.
  int read_size = this->packet_.Unpacket(this->receive_buffer_.ReadPointer(),
         this->receive_buffer_.ReadSize(), message, max_size);
  if(read_size > 0) {
    this->receive_buffer_.ReadShift(read_size);
  }
  return read_size;
}

int TCPBufferSocketCommunicator::Send() {
  uint32 read_size = this->send_buffer_.ReadSize();
  int send_size = send(this->socket_, this->send_buffer_.ReadPointer(), read_size, MSG_NOSIGNAL);
  if(send_size < 0) {
    LOG(ERROR) << "Failed to send data from buffer.";
    return send_size;
  }
  this->send_buffer_.ReadShift(send_size);
  return send_size;
}

int TCPBufferSocketCommunicator::Receive() {
  if(this->CheckAvailable() == false) {
    return 0;
  }
  if(this->receive_buffer_.PreWrite(this->receive_buffer_.GetUnitBufferSize()) == false) {
    LOG(ERROR) << "Failed to Receive, receive_buffer_ size is not enough!";
    return -1;
  }
  int size = recv(this->socket_, this->receive_buffer_.WritePointer(),
         this->receive_buffer_.WriteSize(), MSG_NOSIGNAL);
  if(size == 0) {
    // Socket shutdown, set unavailable.
    this->SetUnavailable();
    return 0;
  } else if(size < 0){
    return -1;
  } else {
    this->receive_buffer_.WriteShift(size);
    return size;
  }
}

void TCPBufferSocketCommunicator::LoopReceive() {
  while(this->Receive() > 0);
}

}  // namespace amz

